package com.example.config;


import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
@Slf4j
public class redisconfig {


    @Bean//连接redis
    /*这两个注解就是形参没有对象就不会执行*/
//    @ConditionalOnSingleCandidate
//    @ConditionalOnMissingBean
    public RedisTemplate<String,Object> RedisConfig(RedisConnectionFactory redisConnectionFactory)
    {
        log.info("开始创建redis模板");
        //设置redis的连接口工厂对象
        RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
        //连接工厂
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        //序列化（防止乱码）序列化 (Serialization)是将对象的状态信息转换为可以存储或传输的形式的过程。
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new StringRedisSerializer());
        return redisTemplate;
    }
}
